package com.seek.inter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seek.cache.CacheUtil;
import com.seek.common.handler.SpringContextHolder;
import com.seek.model.dto.ResultInfo;
import com.seek.model.entity.TActivity;
import com.seek.service.ActivityService;
import com.seek.service.ReadService;
import com.seek.util.CommonUtil;
import com.seek.util.JsonUtil;

/**
 * 本类主要是作为活动相关接口实现类
 *
 */
public class ActivityInter {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private ActivityService activityService = (ActivityService)SpringContextHolder.getBean("activityService");
	private ReadService readService = (ReadService)SpringContextHolder.getBean("readService");
	
	/**
	 * 获取活动列表
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getActivityList(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		String title = reqJson.getString("title");
		String status = reqJson.getString("status");
		
		// 拼接检索条件
		StringBuffer where = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		params.add(companyId);
		if (!CommonUtil.isEmpty(title)){
			title = "%"+title+"%";
			where.append(" and a.title like ?");
			params.add(title);
		}
		if (!CommonUtil.isEmpty(status)){
			where.append(" and a.status=?");
			params.add(CommonUtil.getIntValue(status));
		}
		
		// 取得总记录行数
		String sql = "select count(1) cnt from t_activity a where is_used=1 and a.company_id=?"+where.toString();
		// 列表数据SQL: 把最需要审核的放在前面
		StringBuffer sb = new StringBuffer("select a.id actId,a.status,a.title,a.begin_time startTime,a.end_time endTime,a.image_url coverImg");
		sb.append(" from t_activity a where is_used=1 and a.company_id=?"+where.toString()+" order by id desc");
				
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(readService.pageQueryBySql(sb.toString(), sql, reqJson.getIntValue("pageSize"), reqJson.getIntValue("pageNo"), params.toArray()));
		return result;
	}
	
	/**
	 * 新增或编辑活动后的保存
	 */
	public ResultInfo saveActivity(HttpServletRequest request,JSONObject reqJson) throws Exception{
		ResultInfo result = new ResultInfo();
		try{
			TActivity en = activityService.save(reqJson);
			Map<String, Object> ret = new HashMap<String, Object>();
			ret.put("id", en.getId());
			result.setStatus(0);
			result.setMessage("成功");
			result.setBody(ret);
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	/**
	 * 获取活动详情
	 * @param request
	 * @param reqJson
	 * @return
	 * @throws Exception
	 */
	public ResultInfo getActivityDetail(HttpServletRequest request, JSONObject reqJson) throws Exception{
		ResultInfo result = new ResultInfo();
		String actId = reqJson.getString("actId");
		
		try{
			if (CommonUtil.isEmpty(actId)) {
				result.setStatus(1);
				result.setMessage("参数错误，请检查!");
				return result;
			}
			
			StringBuffer sb = new StringBuffer("select a.id actId,a.status,a.title,a.begin_time startTime,a.end_time endTime,a.image_url coverImg ");
			sb.append(" from t_activity a where is_used=1 and a.id=?");
			result.setStatus(0);
			result.setMessage("成功");
			result.setBody(readService.findOneMapBySql(sb.toString(), new Object[]{actId}));
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	/**
	 * 活动上下架
	 * @param request
	 * @param reqJson
	 * @return
	 * @throws Exception
	 */
	public ResultInfo setActivityStatus(HttpServletRequest request, JSONObject reqJson) throws Exception{
		ResultInfo result = new ResultInfo();
		String actId = reqJson.getString("actId");
		int status = CommonUtil.getIntValue(reqJson.getString("status"));
		try{
			if (CommonUtil.isEmpty(actId)) {
				result.setStatus(1);
				result.setMessage("参数错误，请检查!");
				return result;
			}
			
			String sql = "update t_activity set status=? where id =?";
			activityService.executeUpdate(sql,new Object[]{status,actId});
			CacheUtil.delRedisData("ACTIVITY_ONLINE_"+reqJson.getString("companyId"));

			result.setStatus(0);
			result.setMessage("成功");
			return result;
		}catch(Exception e){
			logger.error("系统异常",e);
			result.setStatus(1);
			result.setMessage(e.getMessage());
			return result;
		}
	}
	
	/**
	 * 获取app中首页显示图片
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public ResultInfo getActivityImgs(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		Long companyId = reqJson.getLong("companyId");
		
		List cached = JSONArray.parseArray(CacheUtil.getRedisData("ACTIVITY_ONLINE_"+companyId), Map.class);
		if (cached == null){
			String sql = "select a.id actId, a.image_url coverImg from t_activity a where a.status=1 and a.is_used=1 and company_id=? and a.begin_time<now() and a.end_time>now()";
			cached = readService.findMapBySql(sql, new Object[]{companyId});
			CacheUtil.putRedisData("ACTIVITY_ONLINE_"+companyId, JsonUtil.toJson(cached));
		}
		// 取得总记录行数
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(cached);
		return result;
	}
}
